package com.daodao.daodaoschedule.infrastructure.util.converters;

import ma.glasnost.orika.converter.BidirectionalConverter;
import ma.glasnost.orika.metadata.Type;

/**
 * @author mal
 * @date 2023-02-14 17:18
 */
public class StringBooleanConverter extends BidirectionalConverter<String,Boolean> {
    @Override
    public Boolean convertTo(String source, Type<Boolean> destinationType) {
        if ("Y".equals(source) || "true".equalsIgnoreCase(source)) {
            return Boolean.TRUE;
        } else if ("N".equals(source) || "false".equalsIgnoreCase(source)) {
            return Boolean.FALSE;
        }
        throw new IllegalStateException("Unknown value!");

    }

    @Override
    public String convertFrom(Boolean source, Type<String> destinationType) {
        if (Boolean.TRUE.equals(source)) {
            return "Y";
        } else if (Boolean.FALSE.equals(source)) {
            return "N";
        }
        throw new IllegalStateException("Unknown value!");
    }
}
